package Top200;

/**
 * @author zhangmin
 * @create 2022-02-17 10:43
 * merge操作找到第k个元素即可
 */
public class findMedianSortedArrays04 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n=nums1.length+nums2.length;
        int k=n/2;
        int[] m=new int[n/2+1];
        int i=0,j=0;
        for (int l = 0; l < m.length; l++) {
            if (i<nums1.length&&j<nums2.length){
                if (nums1[i]<nums2[j]){
                    m[l]=nums1[i];
                    i++;
                }else {
                    m[l]=nums2[j];
                    j++;
                }
            }else if (i==nums1.length){
                m[l]=nums2[j];
                j++;
            }else {
                m[l]=nums1[i];
                i++;
            }
            if (l==k){
                if (n%2==1) return m[l];
                else return (m[l]+m[l-1])/2.0;
            }
        }
        return 0.0;
    }
}
